package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.DoctorMapper;
import com.ten.mall.mapper.UserMapper;
import com.ten.mall.service.UserService;
import com.ten.mall.utils.CommonUtis;
import com.ten.mall.vo.Doctor;
import com.ten.mall.vo.User;
import com.ten.mall.vo.UserExample;
import com.ten.mall.vo.UserExample.Criteria;
import com.ten.mall.vo.UserVo;
import com.ten.mall.vo.weixin.AccessToken;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private DoctorMapper doctorMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByPages(Integer start, Integer size, UserVo vo) {
		Map<String,Object> map =new HashMap<String, Object>();
		PageHelper.startPage(start, size);
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(vo.getUsername())){
			c.andWeNameLike("%"+vo.getUsername()+"%");
		}
		if(StringUtils.isNotBlank(vo.getRoleName())){
			c.andRoleNameEqualTo(vo.getRoleName());
		}
		if(StringUtils.isNotBlank(vo.getDoctorId())){
			c.andRemarkEqualTo(vo.getDoctorId());
		}
		if(vo.getState()!=null){
			c.andStateEqualTo(1);
		}
		List<User> list =userMapper.selectByExample(example);
		 PageInfo<User> pageInfo = new PageInfo<>(list);
		map.put("total", pageInfo.getTotal());
		map.put("list", list);
		return map;
	}

	@Override
	public String insertUser(User record) {
		String pss="";
		if(StringUtils.isNotBlank(record.getPassword())){
		   pss=DigestUtils.md5DigestAsHex(record.getPassword().getBytes());
		}
		String id =CommonUtis.createId();
		record.setId(id);
		record.setPassword(pss);
		record.setState(1);
		userMapper.insert(record);
		return id;
	}

	@Override
	@Transactional(readOnly=true)
	public User selectById(String id) {
		User user =userMapper.selectByPrimaryKey(id);
		return user;
	}

	@Override
	public int updateUser(User record) {
		Doctor d =null;
		if("医生".equals(record.getRoleName())){
			d=doctorMapper.selectByPrimaryKey(record.getId());
			if(d==null){
				d =new Doctor();
				d.setId(record.getId());
				d.setDoctorWename(record.getWeName());
				doctorMapper.insert(d);
			}else{
				d.setDoctorWename(record.getWeName());
				doctorMapper.updateByPrimaryKeySelective(d);
			}
		}
		int i =userMapper.updateByPrimaryKeySelective(record);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public User selectByUserVo(String weName) {
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		c.andNameEqualTo(weName);
		List<User> list =userMapper.selectByExample(example);
		User u=null;
		if(list!=null&&list.size()>0){
			u=list.get(0);
		}
		return u;
	}
	/**
	 * 用户登录页面
	 */
	@Override
	public User login(String username,String password) {
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		c.andUsernameEqualTo(username);
		//c.andRoleNameEqualTo("管理员");
		List<User> list =userMapper.selectByExample(example);
		User u=null;
		String pss=DigestUtils.md5DigestAsHex(password.getBytes());
		if(list!=null&&list.size()>0){
			if(pss.equals(list.get(0).getPassword())){
				u= list.get(0);
			}
		}
		return u;
	}

	@Override
	@Transactional(readOnly=true)
	public User selectByOpenId(String openId) {
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(openId)){
			c.andNameEqualTo(openId);
		}
		List<User> list =userMapper.selectByExample(example);
		User u=null;
		if(list!=null&&list.size()>0){
			u=list.get(0);
		}
		return u;
	}
	/**
	 * 查询销售员
	 */
	@Override
	@Transactional(readOnly=true)
	public List<User> selectSalemanPages(String name,String roleName) {
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(roleName)){
			c.andRoleNameEqualTo(roleName);//
		}
		if(StringUtils.isNotBlank(name)){
			c.andNameEqualTo(name);
		}
		List<User> list =userMapper.selectByExample(example);
		return list;
	}
	@Override
	@Transactional(readOnly=true)
	public List<Map<String,String>> selectYishen(String name) {
		// TODO Auto-generated method stub
		List<Map<String,String>> map =userMapper.selectYishen(name);
		return map;
	}

	@Override
	@Transactional(readOnly=true)
	public List<User> selectDoctor(Integer start, Integer size,String name) {
		PageHelper.startPage(start, size);
		UserExample example = new UserExample();
		Criteria c =example.createCriteria();
		c.andRemarkEqualTo(name);
		List<User> list =userMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insertToken(AccessToken accessToken) {
		int i =userMapper.insertToken(accessToken);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public AccessToken selectToken() {
		AccessToken accessToken =userMapper.selectToken();
		return accessToken;
	}

	@Override
	public int updateToken(AccessToken accessToken) {
		int i =userMapper.updateToken(accessToken);
		return i;
	}

	@Override
	public int delUser(String id) {
		int i =userMapper.deleteByPrimaryKey(id);
		return i;
	}

}
